package cn.com.pushworld.examination.ui.module;

import java.awt.Dimension;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBox;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import cn.com.infostrategy.ui.common.LookAndFeel;

/**
 * RadioButton or CheckBox with TextArea
 * @author My Dream
 *
 */
public class RBorCBwithTextArea extends JPanel {
	private static final long serialVersionUID = -1312515439265991046L;

	private boolean isRadionButton = true;
	private String name = null;
	private String content = null;
	private JRadioButton radiobutton = null;
	private JCheckBox checkbox = null;
	private JTextArea textarea = null;
	private ButtonGroup group = null;

	public RBorCBwithTextArea(String _name, String _content, boolean _isRadionButton) {
		this(_name, _content, _isRadionButton, null);
	}

	public RBorCBwithTextArea(String _name, String _content, boolean _isRadionButton, ButtonGroup _group) {
		this.name = _name;
		this.content = _content;
		this.isRadionButton = _isRadionButton;
		this.group = _group;
		initalize();
	}

	public void initalize() {
		this.setLayout(null);
		if (isRadionButton) {
			radiobutton = new JRadioButton(name);
			radiobutton.setFocusable(false);
			this.add(radiobutton);
			radiobutton.setBounds(1, 1, 37, 20);
			group.add(radiobutton);
		} else {
			checkbox = new JCheckBox(name);
			checkbox.setFocusable(false);
			this.add(checkbox);
			checkbox.setBounds(1, 1, 37, 20);
		}
		textarea = new JTextArea();
		textarea.setText(content);
		textarea.setOpaque(false);
		textarea.setLineWrap(true);
		textarea.setEditable(false);
		JScrollPane scrollPane = new JScrollPane(textarea);
		scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		scrollPane.setBorder(BorderFactory.createLineBorder(LookAndFeel.compBorderLineColor, 1));
		this.add(scrollPane);
		scrollPane.setBounds(38, 2, 520, 60);
		this.setPreferredSize(new Dimension(400, 65));
	}

	public boolean isSelected() {
		if (isRadionButton) {
			return radiobutton.isSelected();
		} else {
			return checkbox.isSelected();
		}
	}

	public JRadioButton getRadiobutton() {
		return radiobutton;
	}

	public String getName() {
		return name;
	}

	public void setSelected(boolean _selected) {
		if (isRadionButton) {
			radiobutton.setSelected(true);
		} else {
			checkbox.setSelected(true);
		}
	}
}
